#include "gray.h"

_gray_state gray_state;

float gray_status[2] = {0};
float gray_status_backup[2][20] = {0}; //灰度传感器状态与历史值
uint32_t gray_status_worse = 0;	//灰度管异常状态计数器

/***************************************************
 函数名: void gpio_input_init(void)
 说明: 12路灰度管gpio检测初始化
 入口: 无
 出口: 无
 备注: 无
 作者: 无名创新,
 二创:Auto_Inhere
 ****************************************************/

void gpio_input_init (void)	//初始化
{
    adc_init(ADC2_CH15_A49, ADC_12BIT);
    adc_init(ADC2_CH14_A48, ADC_12BIT);
    adc_init(ADC2_CH13_A47, ADC_12BIT);
    adc_init(ADC2_CH12_A46, ADC_12BIT);
    adc_init(ADC2_CH11_A45, ADC_12BIT);
    adc_init(ADC2_CH10_A44, ADC_12BIT);
    adc_init(ADC2_CH7_A39, ADC_12BIT);
    adc_init(ADC2_CH6_A38, ADC_12BIT);
    adc_init(ADC2_CH5_A37, ADC_12BIT);
    adc_init(ADC2_CH4_A36, ADC_12BIT);
    adc_init(ADC2_CH3_A35, ADC_12BIT);
    adc_init(ADC1_CH9_A25, ADC_12BIT);
}

uint8 adc_to_gpio (adc_channel_enum vadc_chn, uint16 gate)	//把adc采集的信息模拟成gpio高低电平信号
{
    if (adc_convert(vadc_chn) > gate)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

/***************************************************
 函数名: void gpio_input_check_channel_12(void)
 说明: 12路灰度管gpio检测
 入口: 无
 出口: 无
 备注: 无
 作者: 无名创新
 二创:Auto_Inhere
 ****************************************************/

void gpio_input_check_channel_12 (void)	//12路阈值
{
    gray_state.gray.bit1 = adc_to_gpio(ADC2_CH10_A44, 4000);
    gray_state.gray.bit2 = adc_to_gpio(ADC2_CH3_A35, 4000);
    gray_state.gray.bit3 = adc_to_gpio(ADC2_CH11_A45, 4000);
    gray_state.gray.bit4 = adc_to_gpio(ADC2_CH4_A36, 4000);

    gray_state.gray.bit5 = adc_to_gpio(ADC2_CH12_A46, 4000);
    gray_state.gray.bit6 = adc_to_gpio(ADC2_CH5_A37, 4000);
    gray_state.gray.bit7 = adc_to_gpio(ADC2_CH13_A47, 4000);
    gray_state.gray.bit8 = adc_to_gpio(ADC2_CH6_A38, 4000);

    gray_state.gray.bit9 = adc_to_gpio(ADC2_CH14_A48, 4000);
    gray_state.gray.bit10 = adc_to_gpio(ADC2_CH7_A39, 4000);
    gray_state.gray.bit11 = adc_to_gpio(ADC2_CH15_A49, 4000);
    gray_state.gray.bit12 = adc_to_gpio(ADC1_CH9_A25, 4000);

    for (uint16_t i = 19; i > 0; i--)
    {
        gray_status_backup[0][i] = gray_status_backup[0][i - 1];
    }
    gray_status_backup[0][0] = gray_status[0];
    switch (gray_state.state)
    {
        case 0x0001 :
            gray_status[0] = -11;
            gray_status_worse /= 2;
            break;									//000000000001b
        case 0x0003 :
            gray_status[0] = -10;
            gray_status_worse /= 2;
            break;									//000000000011b
        case 0x0002 :
            gray_status[0] = -9;
            gray_status_worse /= 2;
            break;									//000000000010b
        case 0x0006 :
            gray_status[0] = -8;
            gray_status_worse /= 2;
            break;									//000000000110b
        case 0x0004 :
            gray_status[0] = -7;
            gray_status_worse /= 2;
            break;									//000000000100b
        case 0x000C :
            gray_status[0] = -6;
            gray_status_worse /= 2;
            break;									//000000001100b
        case 0x0008 :
            gray_status[0] = -5;
            gray_status_worse /= 2;
            break;									//000000001000b
        case 0x0018 :
            gray_status[0] = -4;
            gray_status_worse /= 2;
            break;									//000000011000b
        case 0x0010 :
            gray_status[0] = -3;
            gray_status_worse /= 2;
            break;									//000000010000b
        case 0x0030 :
            gray_status[0] = -2;
            gray_status_worse /= 2;
            break;									//000000110000b
        case 0x0020 :
            gray_status[0] = -1;
            gray_status_worse /= 2;
            break;									//000000100000b
        case 0x0060 :
            gray_status[0] = 0;
            gray_status_worse /= 2;
            break;									//000001100000b
        case 0x0040 :
            gray_status[0] = 1;
            gray_status_worse /= 2;
            break;									//000001000000b
        case 0x00C0 :
            gray_status[0] = 2;
            gray_status_worse /= 2;
            break;									//000011000000b
        case 0x0080 :
            gray_status[0] = 3;
            gray_status_worse /= 2;
            break;									//000010000000b
        case 0x0180 :
            gray_status[0] = 4;
            gray_status_worse /= 2;
            break;									//000110000000b
        case 0x0100 :
            gray_status[0] = 5;
            gray_status_worse /= 2;
            break;									//000100000000b
        case 0x0300 :
            gray_status[0] = 6;
            gray_status_worse /= 2;
            break;									//001100000000b
        case 0x0200 :
            gray_status[0] = 7;
            gray_status_worse /= 2;
            break;									//001000000000b
        case 0x0600 :
            gray_status[0] = 8;
            gray_status_worse /= 2;
            break;									//011000000000b
        case 0x0400 :
            gray_status[0] = 9;
            gray_status_worse /= 2;
            break;									//010000000000b
        case 0x0C00 :
            gray_status[0] = 10;
            gray_status_worse /= 2;
            break;									//110000000000b
        case 0x0800 :
            gray_status[0] = 11;
            gray_status_worse /= 2;
            break;									//100000000000b
        case 0x0000 :
            gray_status[0] = gray_status_backup[0][0];
            gray_status_worse++;
            break; //00000000b
        default : //其它特殊情况单独判断
        {
            gray_status[0] = gray_status_backup[0][0];
            gray_status_worse++;
        }
    }

    static uint16_t tmp_cnt = 0;
    switch (gray_state.state)
    //停止线检测
    {
        case 0x0030 :
            tmp_cnt++;
            break; //000000110000b
        case 0x0020 :
            tmp_cnt++;
            break; //000000100000b
        case 0x0060 :
            tmp_cnt++;
            break; //000001100000b
        case 0x0040 :
            tmp_cnt++;
            break; //000001000000b
        case 0x00C0 :
            tmp_cnt++;
            break; //000011000000b
        case 0x00F0 : //000011110000b
        {
            if (tmp_cnt >= 10)
            {
                tmp_cnt = 0;
            }
        }
            break;
    }
}

void IPS12 () //12路显示
{
    gray_state.gray.bit1 = adc_to_gpio(ADC2_CH10_A44, 4000);
    gray_state.gray.bit2 = adc_to_gpio(ADC2_CH3_A35, 4000);
    gray_state.gray.bit3 = adc_to_gpio(ADC2_CH11_A45, 4000);
    gray_state.gray.bit4 = adc_to_gpio(ADC2_CH4_A36, 4000);
    gray_state.gray.bit5 = adc_to_gpio(ADC2_CH12_A46, 4000);
    gray_state.gray.bit6 = adc_to_gpio(ADC2_CH5_A37, 4000);
    gray_state.gray.bit7 = adc_to_gpio(ADC2_CH13_A47, 4000);
    gray_state.gray.bit8 = adc_to_gpio(ADC2_CH6_A38, 4000);
    gray_state.gray.bit9 = adc_to_gpio(ADC2_CH14_A48, 4000);
    gray_state.gray.bit10 = adc_to_gpio(ADC2_CH7_A39, 4000);
    gray_state.gray.bit11 = adc_to_gpio(ADC2_CH15_A49, 4000);
    gray_state.gray.bit12 = adc_to_gpio(ADC1_CH9_A25, 4000);
}
